package com.example.tbs;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

public class FileDisplayActivity extends BaseActivity {
    private static final String URL = "url";
    SuperFileView2 mSuperFileView;

    private String filePath;

    public static void launch(Context context, String url ) {
        Intent intent = new Intent(context, FileDisplayActivity.class);
        intent.putExtra(URL, url);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_file);

        init();
    }

    public void init() {
        mSuperFileView = (SuperFileView2) findViewById(R.id.mSuperFileView);
        mSuperFileView.setOnGetFilePathListener(new SuperFileView2.OnGetFilePathListener() {
            @Override
            public void onGetFilePath(SuperFileView2 mSuperFileView2) {
                getFilePathAndShowFile(mSuperFileView2);
            }
        });

        filePath = getIntent().getStringExtra(URL);
        if (!TextUtils.isEmpty(filePath)) {
            LogUtils.d("文件path:" + filePath);
            setFilePath(filePath);
        } else {
            ToastUtils.showShort("没有获取到文件地址");
        }

        mSuperFileView.show();

    }


    private void getFilePathAndShowFile(SuperFileView2 mSuperFileView2) {
        mSuperFileView2.displayFile(FileDisplayUtils.getCacheFile(getFilePath()));
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.d("FileDisplayActivity-->onDestroy");
        if (mSuperFileView != null) {
            mSuperFileView.onStopDisplay();
        }
    }


    public void setFilePath(String fileUrl) {
        this.filePath = fileUrl;
    }

    private String getFilePath() {
        return filePath;
    }



}
